package com.zhuss.job.processor;

import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.job.job.DeleteJobAppendBo;
import com.zhuss.bean.bo.job.job.wechat.CollectJobBo;
import com.zhuss.bean.vo.job.job.DetailJobAppendVo;
import com.zhuss.bean.vo.job.job.DetailJobVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;
import com.zhuss.util.JsonUtil;

/**
 * 取消收藏职位 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("jobDelCollect")
public class JobDelCollect extends AbstractProcessor<Object, CollectJobBo> implements Processor<Object, CollectJobBo> {

    @Autowired
	@Qualifier("jobRead")
	private JobRead jobRead;
    
    @Autowired
	@Qualifier("jobAppendRead")
	private JobAppendRead jobAppendRead;
    
    @Autowired
    @Qualifier("jobAppendDelete")
    private Processor<Object, DeleteJobAppendBo> jobAppendDelete;
    
    /**
     * 校验
     */
    @Override
    public void check(CollectJobBo bo) throws WpsException {
    	Long jobId = bo.getJobId();
    	Integer appendType = bo.getAppendType();
    	
    	DetailJobAppendVo result = jobAppendRead.getByType(appendType, jobId, bo.getConcurrentUserId());
    	
    	if (result == null) {
    		throw new WpsException(ExceptionCode.职位未收藏);
    	}
    	
    }
    
    /**
     * 操作方法
     * @throws WpsException 
     */
    @Override
    public Object process(CollectJobBo param) throws WpsException {
    	Long jobId = param.getJobId();
    	Integer appendType = param.getAppendType();
    	
    	DetailJobAppendVo result = jobAppendRead.getByType(appendType, jobId, param.getConcurrentUserId());
    	
    	DetailJobVo job = ((DetailJobVo) jobRead.detail(jobId));
    	String name = job.getTitle();
    	
    	DeleteJobAppendBo bo = new DeleteJobAppendBo();
    	BeanUtils.copyProperties(param, bo);
    	bo.setJobAppendId(result.getJobAppendId());
    	jobAppendDelete.process(bo);
    	
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(CollectJobBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.职位.key());
    	record.setAction(LogActionEnum.职位取消收藏.key());
    	record.setTargetId(param.getJobId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
